(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open Pycaml
open Python_tools

(*-----------------------------------------------------------------------*)

let py_arithmetic_module =
  python_interfaced_function
    [| |]
    (fun _py_args -> pythonize_string XNum.module_name)

(*-----------------------------------------------------------------------*)

let load_plugin ~plugin_path:path =
  try
    Dynlink.loadfile_private (Dynlink.adapt_filename path)
  with
    | Dynlink.Error e -> failwith (Dynlink.error_message e)

let py_load_plugin =
  python_interfaced_function
    [| EitherStringType |]
    (fun py_args ->
       load_plugin ~plugin_path:(unpythonize_string py_args.(0));
       pynone ())

(*-----------------------------------------------------------------------*)

let py_set_debug_flags =
  python_interfaced_function
    [| EitherStringType |]
    begin
      fun py_args ->
        let flag_string = unpythonize_string py_args.(0) in
        let handle_flag flag =
          match flag with
            | 'a' -> ALParseState.tracing_al_commands := true
            | 'b' -> Machine.tracing_bytecode := true
            | 'e' -> Evaluate.tracing_engine := true
            | 'i' -> ParseState.tracing_input := true
            | 'l' -> ParLayout.tracing_line_breaks := true
            | 'm' -> ParseState.tracing_macros := true
            | 'g' -> AreaGalley.tracing_page_breaks := true
            | 'p' -> PageLayout.tracing_page_layout := true
            | 's' -> ParseState.tracing_stacks := true
            | _   -> ()
        in
        let _ = String.iter handle_flag flag_string in
          pynone ()
    end

(*-----------------------------------------------------------------------*)

let _ =
  register_for_python
    [|
      ("arithmetic_module", py_arithmetic_module);
      ("load_plugin",       py_load_plugin);
      ("set_debug_flags",   py_set_debug_flags);
    |]

(*-----------------------------------------------------------------------*)

let main () =
  Random.self_init ();
  UString.set_string_format `UTF8;
  let kt = py_module "kt" in
  let py_args = pylist_fromarray (Array.map pythonize_string Sys.argv) in
  let exit_code = py_module_call kt "kt_main" (pytuple_fromsingle py_args) in
    pyint_asint exit_code

let _ = let exit_code = main () in exit exit_code

(*-----------------------------------------------------------------------*)
